package com.sunlg.smarthome.ui.plant;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sunlg.smarthome.R;
import com.sunlg.smarthome.data.entity.Pump;

import java.util.List;

/**
 * Created by sunlg on 2017/5/27.
 */

public class PumpAdapter extends RecyclerView.Adapter implements View.OnClickListener{

    private Context context;
    private List<Pump> pumps;
    private OnItemClickListener listener;

    public PumpAdapter(Context context, List<Pump> pumps) {
        this.context = context;
        this.pumps = pumps;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_pump, parent, false);
        view.setOnClickListener(this);

        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ItemViewHolder viewHolder = (ItemViewHolder)holder;
        viewHolder.itemView.setTag(position);

        Pump pump = pumps.get(position);
        viewHolder.tvDesc.setText(pump.getDescription());
    }

    @Override
    public int getItemCount() {
        return pumps.size();
    }

    @Override
    public void onClick(View v) {
        this.listener.onItemClick(v, (int)v.getTag());
    }

    private static class ItemViewHolder extends RecyclerView.ViewHolder{

        private TextView tvDesc;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tvDesc = (TextView)itemView.findViewById(R.id.tv_desc);
        }
    }

    public void setOnItemClicListener(OnItemClickListener listener){
        this.listener = listener;
    }

    public interface OnItemClickListener{
        void onItemClick(View view, int postion);
    }
}
